package linter.linterjava.controller.article;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import linter.linterjava.POJO.DO.Article;
import linter.linterjava.POJO.DTO.ArticleDTO;
import linter.linterjava.POJO.DTO.UserDTO;
import linter.linterjava.POJO.UtilDTO.PageInfoDTO;
import linter.linterjava.POJO.UtilDTO.TypeDTO;
import linter.linterjava.custom.ResultVO;
import linter.linterjava.service.ArticleService;
import linter.linterjava.service.UserQueryService;
import linter.linterjava.service.UserService;
import linter.linterjava.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.List;

import static linter.linterjava.custom.CustomResultType.USER_NOT_EXIST;

/**
 * @author 北煜
 */
@Slf4j
@RestController
@Api(tags = "博客模块")
@RequestMapping("blog")
public class BaseArticleController {

    @Resource
    UserService userService;
    @Resource
    ArticleService articleService;
    private final UserQueryService userQueryService;
    @Autowired
    public BaseArticleController(UserQueryService userQueryService) { this.userQueryService = userQueryService; }
    @GetMapping("/show/getUserByUsername/{username}")
    @ApiOperation("根据用户名查找用户")
    public ResultVO<UserDTO> getUserByUsername(@PathVariable String username) { return userQueryService.getUserByUsername(username); }

    /**
     * @param articleDTO 文章类 -- > 文章内容、文章名称、简介、封面
     * @return
     */
    @PostMapping("createArticle")
    @ApiOperation("创建文章")
    public ResultVO<String> createArticle(@RequestBody ArticleDTO articleDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.createArticle(articleDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("delArticle")
    @ApiOperation("删除文章")
    public ResultVO<String> delArticle(@RequestParam Long articleId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.delArticle(articleId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param articleDTO 文章类 -- > 文章内容、文章名称、简介、封面
     */
    @PostMapping("updateArticle")
    @ApiOperation("更新文章")
    public ResultVO<String> updateArticle(@RequestBody ArticleDTO articleDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.updateArticle(articleDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    /**
     * @param pageInfoDTO  分页+需要查询的类型 hot/recommend/myself
     */
    @PostMapping("getArticle")
    @ApiOperation("获取文章列表")
    public ResultVO<PageInfo<Article>> getArticle(@RequestBody PageInfoDTO pageInfoDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.getArticle(pageInfoDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("queryArticle")
    @ApiOperation("搜索文章 -- 名字、简介、内容、id")
    public ResultVO<List<Article>> queryArticle(@RequestBody ArticleDTO articleDTO){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.queryArticle(articleDTO, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }

    @PostMapping("getArticleById")
    @ApiOperation("通过id获取文章")
    public ResultVO<Article> getArticleById(@RequestParam Long articleId){
        String username = JwtUtils.getUserName((String) SecurityUtils.getSubject().getPrincipal());
        Long uuid = userService.getIdByUsername(username);
        if (uuid != null){ return ResultVO.success(articleService.getArticleById(articleId, uuid)); }
        return ResultVO.error(USER_NOT_EXIST);
    }




}
